package com.nashuju.web.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.durcframework.core.JsonObjProcessor;
import org.durcframework.core.controller.CrudController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nashuju.web.entity.role.Role;
import com.nashuju.web.entity.sys.AddOperateParam;
import com.nashuju.web.entity.sys.SysFunction;
import com.nashuju.web.entity.sys.SysFunctionSch;
import com.nashuju.web.entity.sys.SysOperate;
import com.nashuju.web.entity.sys.SysRes;
import com.nashuju.web.service.login.AddOperateService;
import com.nashuju.web.service.role.RolePermissionService;
import com.nashuju.web.service.role.RoleService;
import com.nashuju.web.service.sys.SysFunctionService;
import com.nashuju.web.service.sys.SysOperateService;
import com.nashuju.web.service.sys.SysResService;

@Controller
public class SysFunctionController extends CrudController<SysFunction, SysFunctionService> {
    @Autowired
    private SysOperateService sysOperateService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private AddOperateService addOperateService;
    @Autowired
    private SysResService resService;
    @Autowired
    private RolePermissionService permissionService;

    /**
     * 设置菜单的操作点
     * @param addOperateParam
     * @return
     */
    @RequestMapping("/addRSysFunction.do")
    public @ResponseBody Object addRSysFunction(AddOperateParam addOperateParam) {
        SysOperate operate = sysOperateService.get(addOperateParam.getOperateCode());
        if (operate == null) {
            return error("操作类型不存在");
        }
        SysRes res = resService.get(addOperateParam.getSrId());
        if (res == null) {
            return error("资源不存在");
        }

        if (this.getService().isExistSysFun(addOperateParam.getOperateCode(), addOperateParam.getSrId())) {
            return error("操作点已添加");
        }

        addOperateService.add(res, operate);

        return success();
    }

    @RequestMapping("/listRSysFunction.do")
    public @ResponseBody Object listRSysFunction(SysFunctionSch searchEntity) {

        return this.queryWithProcessor(searchEntity, new JsonObjProcessor<SysFunction>() {
            @Override
            public void process(SysFunction entity, Map<String, Object> jsonObject) {
                SysOperate operate = sysOperateService.get(entity.getOperateCode());
                jsonObject.put("operateName", operate.getOperateName());
                jsonObject.put("operateCode", operate.getOperateCode());

                List<Role> roles = roleService.getRolesBySysFunction(entity);
                jsonObject.put("roles", roles);
            }
        });
    }

    @RequestMapping("/setSysFunctionRole.do")
    public @ResponseBody Object setSysFunctionRole(AddOperateParam addOperateParam, int sfId) {
        this.permissionService.setSysFunctionRole(sfId, addOperateParam.getRoleId());
        return success();
    }

    @RequestMapping("/delRSysFunction.do")
    public @ResponseBody Object delRSysFunction(SysFunction sysFunction) {
        return this.delete(sysFunction);
    }

    @RequestMapping("/listOperateUse.do")
    public @ResponseBody Object listOperateUse(String operateCode) {

        List<SysFunction> list = this.getService().getByOperateCode(operateCode);

        Map<String, Object> map = new HashMap<String, Object>();
        boolean operateCodeUsed = CollectionUtils.isNotEmpty(list);

        if (operateCodeUsed) {
            for (SysFunction sysFun : list) {
                SysRes res = resService.get(sysFun.getSrId());
                sysFun.setResName(res.getResName());
            }
        }

        map.put("operateCodeUsed", operateCodeUsed);
        map.put("operateCodeUsedList", list);

        return map;
    }

}
